/*
 *  CESlider.h
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#ifndef __CE_Slider_h__
#define __CE_Slider_h__


#include "cocos2dx_extentions/controls/CESliderInterface.h"

#include "cocos2d.h"
#include <list>

namespace cocosext {


/////////////////////////////////////////////////////
//
/** CESlider 
* Slider control. Returns it's values to registered delegates
*/
/////////////////////////////////////////////////////

class CESlider : public cocos2d::CCLayer, public CESliderInterface
{
protected:
typedef std::list<CESliderDelegate*> CESliderDelegates;

protected:
    CESliderDelegates _delegates;
	int _sliderTag;

    // sprites
    cocos2d::CCSprite* _sprBackNormal; //weak ref
    cocos2d::CCSprite* _sprBackDisabled; //weak ref
    cocos2d::CCSprite* _sprBtnNormal; //weak ref
    cocos2d::CCSprite* _sprBtnDisabled; //weak ref
    //Layers
    cocos2d::CCNode* _btn; //weak ref
    cocos2d::CCNode* _btnRail;
    //Other
    cocos2d::CCTouch* _currentTouch;
    cocos2d::CCPoint  _startPoint;

protected:
    CESlider(cocos2d::CCSprite* backNormal, 
             cocos2d::CCSprite* backDisabled, 
             cocos2d::CCSprite* btnNormal, 
             cocos2d::CCSprite* btnDisabled)
             : _sliderTag(0)
			 , _sprBackNormal(backNormal) 
             , _sprBackDisabled(backDisabled)
             , _sprBtnNormal(btnNormal)
             , _sprBtnDisabled(btnDisabled)
             , _btn(NULL)
             , _btnRail(NULL)
             , _currentTouch(NULL) 
	{ 
		CCAssert(_sprBackNormal != NULL, NULL);
        CCAssert(_sprBackDisabled != NULL, NULL); 
        CCAssert(_sprBtnNormal != NULL, NULL); 
        CCAssert(_sprBtnDisabled != NULL, NULL); 
		this->setIsRelativeAnchorPoint(true);
	}

public:
    ~CESlider() {;}

protected:
    virtual bool init();

public:
    //CCLayer delegate
    void registerWithTouchDispatcher(void);

    //CCTouchDelegate interface
    bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);

// Main interface 
    virtual void onChangeCoords(const cocos2d::CCPoint& currentPoint) = 0;

// CESliderInterface
    /* Enable/disable slider */
    virtual void setIsEnabled(bool enabled);
    virtual bool isEnabled() const { return _sprBackNormal->getIsVisible(); }

    /* Register/unregister delegates */
    virtual void registerDelegate(CESliderDelegate* delegate, bool synchronize);
    virtual void unregisterDelegate(CESliderDelegate* delegate);

	/* Get/set slider tag */
	virtual int getSliderTag() const { return _sliderTag; }
	virtual void setSliderTag(int sliderTag) { _sliderTag = sliderTag; }

protected:
        /* Notify registered delegates*/
    void updateDelegates();
};


/////////////////////////////////////////////////////
//
/** CEHorizontalSlider 
* Horizontal slider
*/
/////////////////////////////////////////////////////

class CEHorizontalSlider : public CESlider 
{
private:
    CEHorizontalSlider(cocos2d::CCSprite* backNormal, 
        cocos2d::CCSprite* backDisabled, 
        cocos2d::CCSprite* btnNormal, 
        cocos2d::CCSprite* btnDisabled): CESlider(backNormal, backDisabled, btnNormal, btnDisabled) {;}

public:
    ~CEHorizontalSlider() {;}

    /* Static constructor */
    static CEHorizontalSlider* node(cocos2d::CCSprite* backNormal, 
        cocos2d::CCSprite* backDisabled, 
        cocos2d::CCSprite* btnNormal, 
        cocos2d::CCSprite* btnDisabled, 
        float margin, 
        float initValue);

protected:
    //Main interface
    bool init(float margin, float initValue);
    void onChangeCoords(const cocos2d::CCPoint& currentPoint);

public:
    //Main interface 
    /* Slider value */
    virtual float getValue() const;
    virtual void setValue(float value);
};

}; // end namespace cocosext

#endif // __CE_Slider_h__
